package com.seeyon.apps.taxFund.controller;

import com.seeyon.apps.commonExt.util.CommonRestUtil;
import com.seeyon.apps.taxFund.api.ShuiJinEnum;
import com.seeyon.apps.taxFund.api.domain.LoginRes;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.util.Base64;
import com.seeyon.ctp.util.annotation.NeedlessCheckLogin;
import com.seeyon.ctp.util.json.JSONUtil;

import cn.hutool.core.lang.Assert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class TaxFundLoginController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(TaxFundLoginController.class);

    /**
     * 单点登录地址
     * @return
     */
    @NeedlessCheckLogin
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
        try {
            User currentUser = AppContext.getCurrentUser();
            Assert.notNull(currentUser, "用户未登录");
            Map<String, Object> params = new HashMap<>();
            params.put("account", Base64.encodeString(currentUser.getLoginName()));
            ResponseEntity<String> post = CommonRestUtil.post(ShuiJinEnum.LOGIN_AUTH, null, params);
            LoginRes loginRes = JSONUtil.parseJSONString(post.getBody(), LoginRes.class);
//            response.sendRedirect(loginRes.getUrl().replace("http://192.168.13.81", "http://218.92.195.114:8091/stms"));
            response.sendRedirect(loginRes.getUrl());
        }catch (Exception e){
            log.error("税金系统【单点登录】异常", e);
        }
        return null;
    }
}
